<?php

namespace app\admin\controller\wwh;

use app\common\controller\Backend;
use think\Db;

class About extends Base
{
    // 表字段结构
    const DEFAULT_ABOUT = [
        'about_title' => '',
        'about_content' => '',
        'about_image' => '',
        'about_video' => '',
        'culture_title1' => '',
        'culture_slogan1' => '',
        'culture_image1' => '',
        'culture_title2' => '',
        'culture_slogan2' => '',
        'culture_image2' => '',
        'culture_title3' => '',
        'culture_slogan3' => '',
        'culture_image3' => '',
        'culture_title4' => '',
        'culture_slogan4' => '',
        'culture_image4' => '',
        'culture_title5' => '',
        'culture_slogan5' => '',
        'culture_image5' => ''
    ];

    // 语言标识
    const LANG_CN = 1;
    const LANG_EN = 2;

    /**
     * 查看
     */
    public function index()
    {
        // 初始化中英文数据
        $dataCN = $this->initAboutData(self::LANG_CN);
        $dataEN = $this->initAboutData(self::LANG_EN);

        $this->assign([
            'dataCN' => $dataCN,
            'dataEN' => $dataEN
        ]);

        return $this->view->fetch();
    }

    /**
     * 初始化数据
     * @param int $lang 语言标识
     * @return array
     */
    protected function initAboutData($lang)
    {
        $id = $lang; // ID与语言标识相同
        $data = Db::name('wwh_about')->where('id', $id)->find();

        if (!$data) {
            $data = array_merge(
                ['id' => $id, 'lang' => $lang],
                self::DEFAULT_ABOUT
            );
            Db::name('wwh_about')->insert($data);
        }

        return $data;
    }

    /**
     * 保存数据
     * @param int $lang 语言标识
     */
    public function saveAbout($lang)
    {
        $id = $lang; // ID与语言标识相同
        $data = [
            'id' => $id,
            'about_title' => input('about_title'),
            'about_content' => input('about_content'),
            'about_image' => input('about_image'),
            'about_video' => input('about_video'),
            'culture_title1' => input('culture_title1'),
            'culture_slogan1' => input('culture_slogan1'),
            'culture_image1' => input('culture_image1'),
            'culture_title2' => input('culture_title2'),
            'culture_slogan2' => input('culture_slogan2'),
            'culture_image2' => input('culture_image2'),
            'culture_title3' => input('culture_title3'),
            'culture_slogan3' => input('culture_slogan3'),
            'culture_image3' => input('culture_image3'),
            'culture_title4' => input('culture_title4'),
            'culture_slogan4' => input('culture_slogan4'),
            'culture_image4' => input('culture_image4'),
            'culture_title5' => input('culture_title5'),
            'culture_slogan5' => input('culture_slogan5'),
            'culture_image5' => input('culture_image5'),
            'lang' => $lang,
        ];

        $exists = Db::name('wwh_about')->where('id', $id)->find();

        if ($exists) {
            // 检查数据变动
            foreach (array_keys(self::DEFAULT_ABOUT) as $field) {
                if ($exists[$field] != $data[$field]) {
                    $result = Db::name('wwh_about')->update($data);
                    return $this->returnResult($result);
                }
            }
            return $this->error('未检测到数据变动', null, null, false);
        }

        $result = Db::name('wwh_about')->insert($data);
        return $this->returnResult($result);
    }

    /**
     * 返回操作结果
     * @param bool $result
     */
    protected function returnResult($result)
    {
        return $result ? $this->success('保存成功') : $this->error('保存失败');
    }

    /**
     * 中文公司概况修改
     */
    public function AboutCN()
    {
        return $this->saveAbout(self::LANG_CN);
    }

    /**
     * 英文公司概况修改
     */
    public function AboutEN()
    {
        return $this->saveAbout(self::LANG_EN);
    }
}
